package cn.exrick.xboot.modules.transport.service.impl;

import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.modules.base.dao.mapper.UserRoleMapper;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.base.entity.UserRole;
import cn.exrick.xboot.modules.base.enums.RoleEnum;
import cn.exrick.xboot.modules.transport.entity.CompanyWarehouses;
import cn.exrick.xboot.modules.transport.mapper.CompanyWarehousesMapper;
import cn.exrick.xboot.modules.transport.mapper.RegionsMapper;
import cn.exrick.xboot.modules.transport.entity.Regions;
import cn.exrick.xboot.modules.transport.param.EditRegionsParam;
import cn.exrick.xboot.modules.transport.param.QueryRegionsParam;
import cn.exrick.xboot.modules.transport.service.CompanyWarehousesService;
import cn.exrick.xboot.modules.transport.service.RegionsService;
import cn.exrick.xboot.modules.transport.vo.RegionsVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地区信息接口实现
 * @author gj
 */
@Slf4j
@Service
@Transactional
public class RegionsServiceImpl extends ServiceImpl<RegionsMapper, Regions> implements RegionsService {

    @Autowired
    private RegionsMapper regionsMapper;
    @Autowired
    private CompanyWarehousesService companyWarehousesService;
    @Autowired
    private CompanyWarehousesMapper companyWarehousesMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRegionsById(Long id) {
        //如果地区下有仓库则不允许删除
        if (companyWarehousesMapper.selectCount(new QueryWrapper<CompanyWarehouses>().eq("region_id", id)) > 0) {
            throw new RuntimeException("该地区下有仓库，不允许删除");
        }
        regionsMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateRegions(EditRegionsParam regions) {
        this.saveOrUpdate(BeanConverterUtil.convertBean(regions, Regions.class));
        //如果变更 地区名称则更新关联的仓点管理员角色
        if (regions.getId() != null) {
            Regions oldRegions = regionsMapper.selectById(regions.getId());
            if (!oldRegions.getRegionName().equals(regions.getRegionName())) {
                userRoleMapper.update(new UserRole().setRoleName(regions.getRegionName() + RoleEnum.WAREHOUSE_ADMIN.getValue()),
                        new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleName, oldRegions.getRegionName() + RoleEnum.WAREHOUSE_ADMIN.getValue()));
            }
        }
        return true;
    }

    @Override
    public IPage<RegionsVo> getByCondition(Page page, QueryRegionsParam param) {
        return regionsMapper.getByCondition(page, param);
    }
}